package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import model.ButtonRequest;
import model.ButtonRequestModel;
import model.ComplaintModel;
import view.NotificationDetailsGUI;
import view.NotificationGUI;
import client.CliMessage;


	/**
	 * This Class is a Notification Controller which extends AbstractController 
	 * @author Tal 
	 *
	 */

	public class NotificationController extends AbstractController 
	{	
				private  NotificationGUI  NotificationView;
				private ButtonRequestModel ButtonRequestModel;
	
				
				/**
				 * This is Notification controller constructor.
				 * @param NotificationView is a Notification window GUI instance.
				 * @param ButtonRequestModel is a ButtonRequest Model instance.
				 */
				public NotificationController( NotificationGUI  NotificationView,ButtonRequestModel ButtonRequestModel) 
				{
					super(NotificationView,ButtonRequestModel);
					this.NotificationView = NotificationView;
					this.ButtonRequestModel = ButtonRequestModel;
				
				}
				
				/**
				 * This is setNotificationButtons method-this method add listeners to buttons in Notification window.
				 * 
				 */
				
				private void setNotificationButtons(ArrayList<ButtonRequest>  btnReq)
				{
					
					int nButtons = btnReq.size();
					
					
					//set a HashMap that contains all possible button listeners by their names
					ArrayList<ButtonListener> al = new ArrayList<ButtonListener>();
					ArrayList<ActionListener>  btnLis=new ArrayList<ActionListener>();
					
					//set  specific complaints buttons
					for(int i = 0; i < nButtons ; i++)
					{
						al.add(new ButtonListener(this,btnReq.get(i).getButtonReqID(),btnReq.get(i).getButtonUID()));
						btnLis.add(al.get(i));
						NotificationView.addButtonListener(btnLis.get(i),i);
					}	
				
				}
							
				
				/**
				 * Inner class that handles when Button Pressed by request Id, extends AbstractListener
				 * @author Tal
				 *
				 */
				
				class ButtonListener extends AbstractListener
				{
					private NotificationController previusObserver;
					private String ComID;
					private String Uid;
					
					public ButtonListener(NotificationController previusObserver, String comid,String uid)
					{
						this.ComID=comid;
						this.Uid=uid;
						this.previusObserver=previusObserver;
					}
					
					@Override
					public void actionPerformed(ActionEvent arg0) 
					{
						
                      
						//create a new NotificationDetails controller
						NotificationDetailsGUI NotDeatilsView = new NotificationDetailsGUI();
						ComplaintModel ComplaintModel = new ComplaintModel(Integer.parseInt(Uid),Integer.parseInt(ComID));
						
						NotificationDetailsController notDetController = new NotificationDetailsController(NotDeatilsView,ComplaintModel);
						
						//add observer to new controller and delete the rest
						setCurrentObserver(notDetController);						
						
						//add a listener to "back" button in NotificationDetails window
						setBackListener(previusObserver,NotDeatilsView);
			
						
						//remove buttons from arrayList
						ButtonRequestModel.RemoveButtonRequest();
							
						String searchQuery = "search"	+"SELECT cusComp.details,cusComp.reply,cusComp.Status,u.fname,u.lname "
								+" FROM customer_complaints AS cusComp, user AS u"
                                +" WHERE cusComp.comid = " + ComID + " AND cusComp.uid=u.uid ";
                               

                          sendToServer(searchQuery);
						
					
					}
				}



				@Override
				public void handleDBRequestSuccess()
				{
					NotificationView.displayWrnMessage("Notification Requests Found:");
					
				}
			
				@Override
				public void handleDBSearchResult(CliMessage dbResult) 
				{	
					//get a list of all Notification
					ArrayList<HashMap<String, String>> srs = dbResult.getSrs();
					ArrayList<ButtonRequest>  btnReq= ButtonRequestModel.getButtonRequest();
					int rows=srs.size();
					int column=3;
					
					//create data table
					 Object[][] data=new Object[rows][column];
				   	
					 String[] columnNames={"Req. Number", "fName", "lName"};

					
					for(int i = 0 ; i <rows  ; i++)
					{
						Map<String, String> complaintsList  = srs.get(i);	
						
	                    //add uid & comId button to arraylist
						btnReq.add(new ButtonRequest(complaintsList.get("COMID"),complaintsList.get("UID")));
								
						//req num
						 data[i][0]=i+1;
						 //fname
						 data[i][1]=complaintsList.get("FNAME");
						 //lname
						 data[i][2]=complaintsList.get("LNAME");
						
						 System.out.println("user id:"+complaintsList.get("ComID")+" "+"user fname:"+complaintsList.get("FNAME")+" "+"user lname:"+complaintsList.get("LNAME"));
							 					
					}	
					
					
			
					NotificationView.setTable(columnNames, data);
					
					//create n buttons
					int numberOfRequests = btnReq.size();
					
					 
					//display main window
					NotificationView.setVisible(true);
					NotificationView.setTableVisibilty(true);		
					
					//create buttons
					NotificationView.setNotificationButtons(numberOfRequests);
					
					//Set list button  listener	
					setNotificationButtons(btnReq);
					 
					//display main window
					NotificationView.setVisible(true);
					NotificationView.setTableVisibilty(true);
							
				}

				@Override
				public void handleNullSearchResult() 
				{
					//display main window
					NotificationView.setVisible(true);
					NotificationView.disposeTable();
					NotificationView.disposeExistingButtons();
					//msg
					NotificationView.displayWrnMessage("Notification Request Not Found!!!");
					
					
				}



}
